#include <iostream>
#include <unordered_map>
#include <string>
#include <utility>

using std::cout;
using std::endl;
using std::unordered_map;
using std::string;
using std::pair;
using std::make_pair;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
}

void test()
{
    //unordered_map的特征
    //1、unordered_map中存放的是pair，也就是key-value类型，
    //key值是唯一的不能重复，但是value是可以重复的,也可以不重复
    //2、key值是没有顺序的
    //3、底层实现也是使用的哈希
    //
    unordered_map<int, string> number = {
        pair<int, string>(8, "wuhan"),
        pair<int, string>(6, "wuhan"),
        pair<int, string>(9, "hubei"),
        {1, "beijing"},
        {1, "dongjing"},
        {2, "nanjing"},
        make_pair(3, "tianjin"),
        make_pair(8, "wuhan"),
        make_pair(10, "shenzhen")
    };
    display(number);

    cout << endl << "unordered_map的下标操作" << endl;
    //map的下标中传的是key类型，然后返回的是value类型
    cout << "number[1] = " << number[1] << endl;//查找
    cout << "number[12] = " << number[12] << endl;//插入
    display(number);

    cout << endl << endl;
    //T &operator[](key)
    number.operator[](12) = "kiki";
    /* number[12] = "kiki";//修改 */
    display(number);
}

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
    }

    ~Point()
    {
    }

    friend std::ostream &operator<<(std::ostream &os, const Point &rhs);

private:
    int _ix;
    int _iy;
};

std::ostream &operator<<(std::ostream &os, const Point &rhs)
{
    os << "(" << rhs._ix 
       << ", " << rhs._iy
       << ")";

    return os;
}

void test2()
{
    unordered_map<string, Point> number = {
        {"hello", Point(1, 2)},
        {"world", Point(2, 2)},
        make_pair("wuhan", Point(3, 2)),
        make_pair("wuhan", Point(3, 2)),
        {"nanjing", Point(1, 2)},
        {"dongjing", Point(1, 2)},
    };
    display(number);
}

int main(int argc, char **argv)
{
    test2();
    return 0;
}

